package com.gisocn.onemap.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.ServiceSettings;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.esri.core.geometry.Geometry;
import com.gisocn.onemap.base.BaseFragment;
import com.gisocn.onemap.base.CommonAdapter;
import com.gisocn.onemap.base.ViewHolder;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.CityStreetData;
import com.gisocn.onemap.model.QueryLocation;
import com.gisocn.onemap.presenter.QueryLocationPresent;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.ui.activity.MainActivity;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.view.QueryLocationView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/* loaded from: classes2.dex */
public class QueryLocationFragment extends BaseFragment<QueryLocationPresent> implements QueryLocationView {
    CommonAdapter adapter;
    CommonAdapter adapterGd;
    CommonAdapter adapterSGCity;
    CommonAdapter adapterSGCityChild;
    AppConfig appConfig;
    @BindView(R.id.et_query)
    EditText et_query;
    @BindView(R.id.iv_expansion)
    ImageView iv_expansion;
    @BindView(R.id.ll_level_child)
    LinearLayout ll_level_child;
    @BindView(R.id.ll_level_group)
    LinearLayout ll_level_group;
    private PoiSearch poiSearch;
    Map<String, Geometry> queryLayerMaps;
    @BindView(R.id.recycle_city_street)
    RecyclerView recycle_city_street;
    @BindView(R.id.recycle_city_street_child)
    RecyclerView recycle_city_street_child;
    @BindView(R.id.recycle_search_gd)
    RecyclerView recycle_search_gd;
    @BindView(R.id.recycle_search_result)
    RecyclerView recycle_search_result;
    @BindView(R.id.rl_city_expan)
    RelativeLayout rl_city_expan;
    @BindView(R.id.tv_child_name)
    TextView tv_child_name;
    @BindView(R.id.tv_close)
    TextView tv_close;
    @BindView(R.id.tv_group_name)
    TextView tv_group_name;
    @BindView(R.id.tv_search)
    TextView tv_search;
    @BindView(R.id.tv_sg)
    TextView tv_sg;
    Unbinder unbinder;
    ArrayList<String> names = new ArrayList<>();
    private ArrayList<PoiItem> searchList = new ArrayList<>();
    private ArrayList<PoiItem> adressList = new ArrayList<>();
    private int searchTag = -1;
    boolean queryEnd = true;
    int spanCountCityName = 1;
    int spWidth = 1;
    boolean cityExpan = false;
    private ArrayList<CityStreetData.StreetGroupData> streetDatas = new ArrayList<>();
    private ArrayList<CityStreetData.StreetGroupData.StreetChildData> streetChildDatas = new ArrayList<>();
    private ArrayList<QueryLocation.ZJDATA> zjdatas = new ArrayList<>();
    private ArrayList<QueryLocation.ZJDATA> zjChildDatas = new ArrayList<>();
    Handler handler = new Handler() { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.6
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what != 1) {
                return;
            }
            if (QueryLocationFragment.this.names.size() == 0) {
                QueryLocationFragment queryLocationFragment = QueryLocationFragment.this;
                queryLocationFragment.queryByGd(queryLocationFragment.getStr(queryLocationFragment.et_query));
            }
            QueryLocationFragment.this.adapter.notifyDataSetChanged();
        }
    };

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void onInvisible() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_query_location, viewGroup, false);
        this.unbinder = ButterKnife.bind(this, inflate);
        DisplayMetrics displayMetrics = inflate.getResources().getDisplayMetrics();
        Log.e("item", "" + inflate.getWidth());
        int i = displayMetrics.widthPixels;
        this.spWidth = i;
        this.spWidth = ((i + (-70)) / 5) + 20;
        return inflate;
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void initData() {
        this.presenter = createPresenter();
        initView();
        AppConfig appConfig = ((MainActivity) getActivity()).getAppConfig();
        this.appConfig = appConfig;
        if (appConfig.getQueryLocation() == null || isNull(this.appConfig.getQueryLocation().getZJData()) || isNull(this.appConfig.getQueryLocation().getZJMCField()) || isNull(this.appConfig.getQueryLocation().getZJGLField())) {
            return;
        }
        ((QueryLocationPresent) this.presenter).queryZJInfo(this.appConfig.getQueryLocation(), 0);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseFragment
    public QueryLocationPresent createPresenter() {
        return new QueryLocationPresent(this);
    }

    @OnClick({R.id.tv_search, R.id.tv_close, R.id.tv_search_bd, R.id.rl_city_expan, R.id.ll_level_group, R.id.ll_level_child, R.id.tv_sg})
    public void onViewClicked(View view) {
        int id = view.getId();
        int i = R.mipmap.icon_expan_up;
        switch (id) {
            case R.id.ll_level_child /* 2131231044 */:
                this.ll_level_child.setVisibility(8);
                this.recycle_city_street_child.setVisibility(8);
                return;
            case R.id.ll_level_group /* 2131231045 */:
            case R.id.tv_sg /* 2131231444 */:
                this.ll_level_group.setVisibility(8);
                this.ll_level_child.setVisibility(8);
                this.recycle_city_street_child.setVisibility(8);
                this.recycle_city_street.setVisibility(0);
                return;
            case R.id.rl_city_expan /* 2131231244 */:
                this.cityExpan = !this.cityExpan;
                RequestManager with = Glide.with(this);
                if (!this.cityExpan) {
                    i = R.mipmap.icon_expan_down;
                }
                with.load(Integer.valueOf(i)).into(this.iv_expansion);
                if (this.recycle_city_street_child.getVisibility() == 0) {
                    this.recycle_city_street_child.setVisibility(this.cityExpan ? 0 : 8);
                    return;
                } else {
                    this.recycle_city_street.setVisibility(this.cityExpan ? 0 : 8);
                    return;
                }
            case R.id.tv_close /* 2131231385 */:
                ((MainActivity) getActivity()).setViewPagerVisibility(false);
                return;
            case R.id.tv_search /* 2131231441 */:
                L.e("点击了本地搜索");
                if (this.queryEnd) {
                    this.searchTag = 0;
                    if (!isNull(getStr(this.et_query))) {
                        this.cityExpan = false;
                        if (this.recycle_city_street.getVisibility() == 0 || this.recycle_city_street_child.getVisibility() == 0) {
                            RequestManager with2 = Glide.with(this);
                            if (!this.cityExpan) {
                                i = R.mipmap.icon_expan_down;
                            }
                            with2.load(Integer.valueOf(i)).into(this.iv_expansion);
                            this.recycle_city_street.setVisibility(8);
                            this.recycle_city_street_child.setVisibility(8);
                        }
                        this.recycle_search_result.setVisibility(0);
                        this.recycle_search_gd.setVisibility(8);
                        queryByGd(getStr(this.et_query));
                        return;
                    }
                    showToast("请输入关键字");
                    return;
                }
                return;
            case R.id.tv_search_bd /* 2131231443 */:
                queryByGd(getStr(this.et_query));
                return;
            default:
                return;
        }
    }

    void initView() {
        this.recycle_search_result.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
        this.recycle_search_gd.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
        int floor = (int) Math.floor(this.spWidth / getResources().getDimensionPixelOffset(R.dimen.dp_100));
        this.spanCountCityName = floor;
        this.recycle_city_street.setLayoutManager(new StaggeredGridLayoutManager(floor, 1));
        this.recycle_city_street_child.setLayoutManager(new StaggeredGridLayoutManager(this.spanCountCityName, 1));
        CommonAdapter<String> commonAdapter = new CommonAdapter<String>(getActivity(), R.layout.item_query_location, this.names) { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // com.gisocn.onemap.base.CommonAdapter
            public void convert(ViewHolder viewHolder, final String str, int i) {
                ((StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams()).setFullSpan(true);
                viewHolder.setText(R.id.tv_name, str);
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.1.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        ((MainActivity) QueryLocationFragment.this.getActivity()).loadGeometryAndFlicker(QueryLocationFragment.this.queryLayerMaps.get(str), "Flicker", 2);
                    }
                });
            }
        };
        this.adapter = commonAdapter;
        this.recycle_search_result.setAdapter(commonAdapter);
        CommonAdapter<PoiItem> commonAdapter2 = new CommonAdapter<PoiItem>(getActivity(), R.layout.item_query_location, this.searchList) { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.2
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // com.gisocn.onemap.base.CommonAdapter
            public void convert(ViewHolder viewHolder, PoiItem poiItem, final int i) {
                ((StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams()).setFullSpan(true);
                viewHolder.setText(R.id.tv_name, poiItem.getTitle());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.2.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        ((MainActivity) QueryLocationFragment.this.getActivity()).localSearchGd(((PoiItem) QueryLocationFragment.this.searchList.get(i)).getLatLonPoint());
                    }
                });
            }
        };
        this.adapterGd = commonAdapter2;
        this.recycle_search_gd.setAdapter(commonAdapter2);
        CommonAdapter<QueryLocation.ZJDATA> commonAdapter3 = new CommonAdapter<QueryLocation.ZJDATA>(getActivity(), R.layout.item_sg_group_name, this.zjdatas) { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.3
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // com.gisocn.onemap.base.CommonAdapter
            public void convert(ViewHolder viewHolder, final QueryLocation.ZJDATA zjdata, int i) {
                viewHolder.setText(R.id.tv_name, zjdata.getMcValue());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.3.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        QueryLocationFragment.this.ll_level_group.setVisibility(0);
                        TextView textView = QueryLocationFragment.this.tv_group_name;
                        textView.setText(zjdata.getMcValue() + "");
                        ((MainActivity) QueryLocationFragment.this.getActivity()).loadGeometryAndFlicker(zjdata.getGeometry(), "Flicker", 2);
                        if (zjdata.getZjChildDatas() != null && zjdata.getZjChildDatas().size() > 0) {
                            QueryLocationFragment.this.zjChildDatas.clear();
                            QueryLocationFragment.this.zjChildDatas.addAll(zjdata.getZjChildDatas());
                            QueryLocationFragment.this.recycle_city_street_child.setVisibility(0);
                            QueryLocationFragment.this.recycle_city_street.setVisibility(8);
                            QueryLocationFragment.this.adapterSGCityChild.notifyDataSetChanged();
                            return;
                        }
                        QueryLocationFragment queryLocationFragment = QueryLocationFragment.this;
                        queryLocationFragment.showToast("无" + zjdata.getMcValue() + "辖下行政信息");
                    }
                });
            }
        };
        this.adapterSGCity = commonAdapter3;
        this.recycle_city_street.setAdapter(commonAdapter3);
        CommonAdapter<QueryLocation.ZJDATA> commonAdapter4 = new CommonAdapter<QueryLocation.ZJDATA>(getActivity(), R.layout.item_sg_group_name, this.zjChildDatas) { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.4
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // com.gisocn.onemap.base.CommonAdapter
            public void convert(ViewHolder viewHolder, final QueryLocation.ZJDATA zjdata, int i) {
                viewHolder.setText(R.id.tv_name, zjdata.getMcValue());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.4.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        QueryLocationFragment.this.recycle_city_street.setVisibility(8);
                        QueryLocationFragment.this.ll_level_child.setVisibility(0);
                        TextView textView = QueryLocationFragment.this.tv_child_name;
                        textView.setText(zjdata.getMcValue() + "");
                        ((MainActivity) QueryLocationFragment.this.getActivity()).loadGeometryAndFlicker(zjdata.getGeometry(), "Flicker", 2);
                    }
                });
            }
        };
        this.adapterSGCityChild = commonAdapter4;
        this.recycle_city_street_child.setAdapter(commonAdapter4);
    }

    private void query(String str) {
        this.queryEnd = false;
        ((QueryLocationPresent) this.presenter).queryLocation(str, this.appConfig.getQueryLocation());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void queryByGd(String str) {
        this.searchTag = 1;
        L.e("地名搜索");
        if (str.length() > 0) {
            this.recycle_search_result.setVisibility(8);
            this.recycle_search_gd.setVisibility(0);
            this.searchList.clear();
            final String trim = this.appConfig.getCity().trim();
            L.e("cityCode______________" + trim);
            ServiceSettings.updatePrivacyShow(getActivity(), true, true);
            ServiceSettings.updatePrivacyAgree(getActivity(), true);
            try {
                PoiSearch poiSearch = new PoiSearch(getActivity(), new PoiSearch.Query(str, "", trim));
                this.poiSearch = poiSearch;
                poiSearch.searchPOIAsyn();
                this.poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() { // from class: com.gisocn.onemap.ui.fragment.QueryLocationFragment.5
                    @Override // com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener
                    public void onPoiItemSearched(PoiItem poiItem, int i) {
                    }

                    @Override // com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener
                    public void onPoiSearched(PoiResult poiResult, int i) {
                        L.e("" + new Gson().toJson(poiResult));
                        Iterator<PoiItem> it = poiResult.getPois().iterator();
                        while (it.hasNext()) {
                            PoiItem next = it.next();
                            if (next.getCityName().equals(trim) || next.getAdName().equals(trim)) {
                                QueryLocationFragment.this.searchList.add(next);
                            }
                        }
                        if (QueryLocationFragment.this.searchList.size() > 0) {
                            QueryLocationFragment.this.adapterGd.notifyDataSetChanged();
                        } else {
                            QueryLocationFragment.this.showToast("暂无搜索结果");
                        }
                    }
                });
                return;
            } catch (AMapException e) {
                e.printStackTrace();
                return;
            }
        }
        showToast("请输入关键字");
    }

    @Override // com.gisocn.onemap.view.QueryLocationView
    public void fail(String str) {
        this.queryEnd = true;
        queryByGd(getStr(this.et_query));
    }

    @Override // com.gisocn.onemap.view.QueryLocationView
    public void queryResult(Map<String, Geometry> map) {
        this.queryEnd = true;
        this.queryLayerMaps = map;
        this.names.clear();
        this.names.addAll(map.keySet());
        this.handler.sendEmptyMessage(1);
    }

    @Override // com.gisocn.onemap.view.QueryLocationView
    public void queryCityInfoResult(ArrayList<QueryLocation.ZJDATA> arrayList, int i) {
        L.e("queryCityInfoResult" + this.zjdatas.size());
        if (i != 0) {
            Iterator<QueryLocation.ZJDATA> it = this.zjdatas.iterator();
            while (it.hasNext()) {
                QueryLocation.ZJDATA next = it.next();
                L.e("镇------" + next.getGlFieldValue());
                Iterator<QueryLocation.ZJDATA> it2 = arrayList.iterator();
                while (it2.hasNext()) {
                    QueryLocation.ZJDATA next2 = it2.next();
                    L.e("村------" + next2.getGlFieldValue() + next2.getMcValue());
                    if (!isNull(next2.getGlFieldValue()) && next2.getGlFieldValue().contains(next.getGlFieldValue())) {
                        if (next.getZjChildDatas() == null) {
                            next.setZjChildDatas(new ArrayList<>());
                        }
                        next.getZjChildDatas().add(next2);
                    }
                }
            }
        } else if (arrayList != null) {
            this.zjdatas.addAll(arrayList);
            if (!isNull(this.appConfig.getQueryLocation().getCJData()) && !isNull(this.appConfig.getQueryLocation().getCJMCField()) && !isNull(this.appConfig.getQueryLocation().getCJGLField())) {
                ((QueryLocationPresent) this.presenter).queryZJInfo(this.appConfig.getQueryLocation(), 1);
            }
        }
        this.adapterSGCity.notifyDataSetChanged();
    }
}
